<template>
  <el-dialog
    title="维修申报"
    :visible.sync="dialogVisible"
    :before-close="handleClose"
    width="30%"
  >
    <el-form :label-position="'left'" ref="form" :model="form" :rules="rules" label-width="140px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="故障描述" prop="problemdesc">
            <el-input v-model="form.problemdesc" type="textarea" :maxlength="200" show-word-limit placeholder="请输入处理详情" :autosize="{minRows: 3, maxRows: 3}" :style="{width: '50'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="反馈图片" prop="image">
            <Upload ref="uploadRef" @success="uploadSuccess"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="维修类型" prop="equipmentType">
              <el-cascader class="el-select-class" filterable clearable  v-model="form.equipmentType" size="small" placeholder="请选择维修类型"  :props="{ multiple: false }" separator="/" :options="getEquipmentType" :show-all-levels="false">
                <template #empty>
                  <div style="padding: 10px; color: #999; text-align: center;">
                    <i class="el-icon-warning"></i> 暂无数据，请稍后再试
                  </div>
                </template>
              </el-cascader>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="申报人单位信息" prop="personFirmInfo">
               <el-select  class="el-select-class" :multiple-limit="3" :debounce="2000"    multiple allow-create @change="changePersonFirmInfo" :disabled="personFirmInfoDisable" v-model="form.personFirmInfo" filterable clearable placeholder="请输入申报人单位信息">
                 <template #prefix>
                   <i class="el-icon-user"></i>
                 </template>
                 <el-option
                  v-for="item in getPersonEquipmentInfo"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                  <span style="float: left; font-size: 13px; color: rgb(93, 150, 232);">{{ item.value }}</span>
                </el-option>
              </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="设备地址" prop="equipAddress">
            <el-select class="el-select-class" size="small" :multiple-limit="1" :debounce="2000"     allow-create   v-model="form.equipAddress" filterable clearable placeholder="请输入设备地址">
              <template #prefix>
                <i class="el-icon-user"></i>
              </template>
              <el-option
                v-for="item in getEquipmentAddress"
                :key="item.value"
                :label="item.label"
                :value="item.value">
                <span style="float: left; font-size: 13px; color: rgb(93, 150, 232);">{{ item.value }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="申报人姓名" prop="name">
            <el-input v-model="form.name" placeholder="请输入申报人姓名" :maxlength="10" :style="{width: '50'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="申报人联系方式" prop="phnumber">
            <el-input v-model="form.phnumber" placeholder="请输入申报人联系方式" :maxlength="11" :style="{width: '50'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="设备故障信息" prop="address">
            <el-input v-model="form.address" placeholder="请输入设备故障信息" :maxlength="100" :style="{width: '50'}">
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="success" @click="save('form')">保 存</el-button>
      <el-button type="primary" @click="resetForm('form')">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { mapGetters ,mapState } from 'vuex'
import Upload from '@/components/Upload';
import * as utilFun from  '@/tool/util'
import * as OrderAction from '@/api/order'
import {Message} from "element-ui";
import {default as  equipments} from  '@/data/index'
import  '@/assets/style/add-dialog.scss'
export default {
  name: "addDialog",
  components: {
    Upload
  },
  watch: {
    "form.personFirmInfo": {
      immediate: true,
      deep: true,
      handler(newValue, oldValue) {
        if (newValue.length > 3) {
        }
      }
    }
  },
  mounted() {
  },
  created() {
    this.form.phnumber =this.userInfoObj.phone
    this.form.name=this.userInfo.userInfo.username;
  },
  computed: {
    ...mapGetters(['userInfoObj']),
    ...mapState(['userInfo']),
    getEquipmentType:()=> {
      // 先获取获取不到用这个默认的
      return   equipments.equipmentType
    },
    getPersonEquipmentInfo:()=>{
      // 先获取获取不到或者不行的话就使用这默认的
      return equipments.personEquipmentInfo
    },
    getEquipmentAddress:()=>{
      // 先获取获取不到或者不行的话就使用这默认的

      let mapListLabel = utilFun.mapListLabel();
      return  mapListLabel ?  mapListLabel : equipments.equipmentAddress ;

    }
  },
  data() {
    return {
      dialogVisible: false, // 弹窗是否显示
      personFirmInfoDisable:false,
      form: {
        problemdesc: '', // 故障描述
        equipmentType: [], //设置类型
        personFirmInfo:[], //申报个人单位信息
        equipAddress: '', // 设备地址
        name: '', // 姓名
        phnumber: '', // 电话
        address: '', // 地址
        imgList: [],
        image: '12311312313123123', // 上传图片后的地址
      },
      rules: {
        problemdesc: [{ required: true, message: '请填写故障描述', trigger: 'blur' }],
        equipAddress: [{ required: true, message: '请填写设备地址', trigger: 'change' }],
        name: [{ required: true, message: '请填写申报人姓名', trigger: 'change' }],
        phnumber: [{required: true, message: '请填写申报人手机号', trigger: 'blur'},{ pattern: /^1[34578]\d{9}$/, message: '请输入正确的手机号码' }],
        company: [{required: true, message: '请填写申报单位信息', trigger: 'change'}],
        address: [{required: true, message: '请填写故障设备信息', trigger: 'change'}],
        equipmentType: [{required: true, message: '请输入维修类型', trigger: 'change'}],
        image: [{required: true, message: '请输入维修图片', trigger: 'change'}],
        personFirmInfo: [{required: true, message: '请输入申报人单位信息', trigger: 'change'}],
      },
    }
  },
  methods: {


    async show() {
      console.log('用户信息', this.userInfoObj);
      this.dialogVisible = true;
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
          this.resetForm('form')
        })
        .catch(_ => {

        });
    },
    // 图片上传
    uploadSuccess (file) {
      // '设置为这个url地址信息内容
      this.$set(this.form, 'image', file.url)
    },
    // 保存
    save(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let insertInfo = { order: {}, userInfo: {} };
          insertInfo.order.address = this.form.address;
          insertInfo.order.description = this.form.problemdesc;
          insertInfo.order.equipId = '408746480478211001';
          insertInfo.order.image = this.form.image;
          insertInfo.order.equipAddress = this.form.equipAddress;
          insertInfo.order.declarantName = this.form.name;
          insertInfo.order.declarantPhone = this.form.phnumber;
          insertInfo.order.declarantCompany = this.form.company;
          insertInfo.order.equipmentType = this.form.equipmentType.length>=2 ? this.form.equipmentType[1]:this.form.equipmentType[0];
          insertInfo.order.personFirmInfo = this.form.personFirmInfo[0];

          insertInfo.userInfo.company = this.form.personFirmInfo;
          insertInfo.userInfo.name = this.form.name;
          insertInfo.userInfo.openId = this.userInfoObj.openId;
          insertInfo.userInfo.phone = this.form.phnumber;
          insertInfo.userInfo.type = this.userInfoObj.ruleType;
          try {
            const res = await OrderAction.sendRepairInfo(insertInfo);
            if (res) {
              this.$message.success('申报成功');
              this.$refs['form'].resetFields();
              this.$emit('reload');
              this.dialogVisible = false;
            }
          }catch (e) {
            Message.error(e.message);
            this.$refs['form'].resetFields();
            this.$refs.uploadRef.fileList = []
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    changePersonFirmInfo(value)
    {
      console.log(value.length)
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.form.image = '';
      this.dialogVisible = false;
      this.form.phnumber = null
      this.form.name = null
    }
  }
}
</script>

<!--<style scoped>-->

<!--</style>-->
